package com.linkstec.bookshop.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linkstec.bookshop.mapper.OrderMapper;
import com.linkstec.bookshop.pojo.Order;
import com.linkstec.bookshop.pojo.User;
import com.linkstec.bookshop.service.CartService;
import com.linkstec.bookshop.service.OrderService;
import com.linkstec.bookshop.util.PageBean;
import com.linkstec.bookshop.util.ToolUtil;
import com.linkstec.bookshop.vo.ScartVo;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderMapper orderMapper;

	@Autowired
	private CartService cartService;

	@Override
	public int toOrder(User user, List<ScartVo> cartList) throws RuntimeException {

		Order order = new Order();

		order.setoState(0);// 0表示没有付款、1表示已付款即将发货 2表示确认收货 3表示交易成功
		order.setUserId(user.getUserId());// 设置userId
		order.setoTime(ToolUtil.getCurrentTime());// 设置订单时间
		double sumMoney = 0;
		for (int i = 0; i < cartList.size(); i++) {
			sumMoney += cartList.get(i).getPrice() * cartList.get(i).getNumber();
		}
		order.setTotalMoney(sumMoney);// 设置订单总金额

		orderMapper.toOrder(order);// 初次添加订单信息

		int[] scids = new int[cartList.size()];
		for (int i = 0; i < cartList.size(); i++) {
			scids[i] = cartList.get(i).getScId();
		}

		cartService.updateOrder(order.getoId(), scids);// 更新购物车信息，设置购物车state为不可用，作为订单附表使用。
		return order.getoId();
	}

	@Override
	public Order findOneOrder(int oId) throws RuntimeException {
		return orderMapper.findOneOrder(oId);
	}

	@Override
	public List<ScartVo> findOrderDetail(int oId) throws RuntimeException {

		return orderMapper.findOrderDetail(oId);
	}

	@Override
	public void payOrder(Order order) throws RuntimeException {
		orderMapper.payOrder(order);
	}

	@Override
	public PageBean<Order> findOrderByUidAndPage(int page, int userId) throws RuntimeException {
		PageBean<Order> pageBean = new PageBean<Order>();

		pageBean.setPage(page);// 设置这是第几页
		int limitPage = 4;
		pageBean.setLimitPage(limitPage);// 设置每页4个
		int totlePage = 0;// 设置一共多少页
		totlePage = orderMapper.countOrderByUid(userId);

		if (Math.ceil(totlePage % limitPage) == 0) {
			totlePage = totlePage / limitPage;
		} else {
			totlePage = totlePage / limitPage + 1;
		}
		pageBean.setTotlePage(totlePage);
		int beginPage = (page - 1) * limitPage;

		List<Order> list = orderMapper.findOrderByUidAndPage(userId, beginPage, limitPage);// 商品集合
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	public void updateState(int oState, int oId) throws RuntimeException {
		orderMapper.updateState(oState, oId);

	}

	@Override
	public PageBean<Order> findAllOrder(int page, int oState) throws RuntimeException {
		PageBean<Order> pageBean = new PageBean<Order>();

		pageBean.setPage(page);// 设置这是第几页
		int limitPage = 4;
		pageBean.setLimitPage(limitPage);// 设置每页4个
		int totlePage = 0;// 设置一共多少页
		totlePage = orderMapper.countOrderByOstate(oState);

		if (Math.ceil(totlePage % limitPage) == 0) {
			totlePage = totlePage / limitPage;
		} else {
			totlePage = totlePage / limitPage + 1;
		}
		pageBean.setTotlePage(totlePage);
		int beginPage = (page - 1) * limitPage;

		List<Order> list = orderMapper.findOrderByOStateAndPage(oState, beginPage, limitPage);// 商品集合
		pageBean.setList(list);
		return pageBean;
	}

}
